package com.yike.order.config;

import cn.hutool.core.io.FileUtil;
import com.github.wxpay.sdk.MyWXPayConfig;
import com.github.wxpay.sdk.WXPay;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WxPayConfiguration {

    @Value("${wx.pay.domain}")
    String domain;
    @Value("${wx.pay.appID}")
    String appID;
    @Value("${wx.pay.mchID}")
    String mchID;
    @Value("${wx.pay.key}")
    String key;
    @Value("${wx.pay.certPath}")
    String certPath;
    @Value("${wx.pay.notifyUrl}")
    String notifyUrl;

    @Bean
    public MyWXPayConfig wxPayConfig() {
        MyWXPayConfig config = new MyWXPayConfig();
        config.setAppID(appID);
        config.setMchID(mchID);
        config.setKey(key);
        config.setDomain(domain);
        config.setCertData(FileUtil.readBytes(certPath));
        return config;
    }

    @Bean
    public WXPay wxpay(MyWXPayConfig wxPayConfig) throws Exception {
//        return new WXPay(wxPayConfig);
        return new WXPay(wxPayConfig, notifyUrl, true, false);
    }
}
